package com.slmxz.tms.document.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.slmxz.tms.common.component.RedisUserService;
import com.slmxz.tms.common.constant.SystemConstants;
import com.slmxz.tms.common.enums.BizMessageEnum;
import com.slmxz.tms.core.exception.Asserts;
import com.slmxz.tms.core.exception.BizException;
import com.slmxz.tms.common.result.ResultCode;
import com.slmxz.tms.core.service.DataScopeService;
import com.slmxz.tms.document.mapper.CargoMapper;
import com.slmxz.tms.document.model.entity.Cargo;
import com.slmxz.tms.document.model.vo.CargoVO;
import com.slmxz.tms.document.service.CargoService;
import com.slmxz.tms.system.model.vo.UserInfoVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import cn.hutool.core.util.StrUtil;

/**
 * 货物信息服务实现类
 * @author sunyao
 * @Date 2024-11-09
 */
@Service
@RequiredArgsConstructor
public class CargoServiceImpl extends ServiceImpl<CargoMapper, Cargo> implements CargoService, DataScopeService<Cargo> {

    /**
    * 获取货物信息分页列表
    * @return {@link IPage< CargoVO >} 货物信息分页列表
    */
    @Override
    public IPage<CargoVO> selectCargoPage(IPage<CargoVO> page,CargoVO cargo) {
        UserInfoVO user = RedisUserService.currentUser();
        try{
            this.dataScopeHandler(cargo);
        }catch (Exception e){
            cargo.setScopeNo(user.getScopeNo());
            cargo.setDataOperate(SystemConstants.OPERATE_EQUALS);
        }
        cargo.setCompanyId(user.getCompanyId());
        return page.setRecords(this.baseMapper.selectCargoPage(page,cargo));
    }
    
    /**
     * @param id 货物信息ID
     * @return
     */
    @Override
    public CargoVO selectCargoDetail(Long id) {
        Cargo entity = this.getById(id);
        CargoVO cargo = BeanUtil.copyProperties(entity,CargoVO.class);
        return cargo;
    }
    
    /**
     * 新增货物信息
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveCargo(Cargo cargo) {
        UserInfoVO user = RedisUserService.currentUser();
        cargo.setCompanyId(user.getCompanyId());
        return this.save(cargo);
    }
    
    /**
     * 更新货物信息
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCargo(Cargo cargo) {
        UserInfoVO user = RedisUserService.currentUser();
        cargo.setCompanyId(user.getCompanyId());
        return this.updateById(cargo);
    }
    
    /**
     * 删除货物信息
     *
     * @param ids 货物信息ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCargos(String ids) {
        Asserts.fail(StrUtil.isBlank(ids), BizMessageEnum.PARAM_IS_NULL);

        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean activeOrLogout(CargoVO cargo) {
        //激活或者注销业务数据
        Asserts.fail((CollectionUtil.isEmpty(cargo.getIds()) || cargo.getIsDeleted() == null), BizMessageEnum.PARAM_IS_NULL);

        List<Cargo>  cargoList = new ArrayList<>(cargo.getIds().size());
        cargo.getIds().forEach(id -> {
            Cargo cargoParam = new Cargo();
            cargoParam.setId(id);
            cargoParam.setIsDeleted(cargo.getIsDeleted());
            cargoList.add(cargoParam);
        });
        this.updateBatchById(cargoList,10);
        return true;
    }

}
